package com.sparrow.mvc;

import java.lang.reflect.Method;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.sparrow.constant.CACHE_KEY;
import com.sparrow.core.Cache;
import com.sparrow.core.MethodParameterNames;
import com.sparrow.ioc.BeanFactory;
import com.sparrow.support.ActionSupport;
import com.sparrow.utils.FileUtil;
import com.sparrow.utils.StringUtil;
import com.sparrow.utils.Xml;

public class ActionConfig {
	private static final ActionConfig config = new ActionConfig();
	private final BeanFactory beanFactory = BeanFactory.getInstance();

	private ActionConfig() {
	}

	public static ActionConfig getActionConfig() {
		return config;
	}

	private Element getActionNodeByAttributeValue(String attributeValue,
			String xmlName) {
		try {
			xmlName = FileUtil.getInstance().getClassesPhysicPath() + xmlName;
			return Xml.getElementByTagAttribute(
					Xml.getXmlDocumentByPath(xmlName, "mvc.dtd"), "action",
					"name", attributeValue);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 通过actionKey获取action实体对象 参数actionKey值格式为 /path1/path2/page.扩展名(html jsp
	 * do) 例:/system/forum/loadAjax.do
	 * 
	 * @author zhanglizhi
	 * @date Aug 2, 2011
	 * @version 1.0
	 * @param actionKey
	 * @param config
	 * @return
	 * @throws Exception
	 */
	public ActionEntity getActionEntity(String actionKey, String xmlName)
			throws Exception {
		ActionEntity actionEntity = new ActionEntity();
		Object oae = Cache.getInstance()
				.get(CACHE_KEY.ACTION_ENTITY, actionKey);
		if (oae == null) {
			Element actionElement = this.getActionNodeByAttributeValue(
					actionKey, xmlName);
			if (actionElement == null) {
				return null;
			}

			String beanName = ((Element) actionElement.getParentNode())
					.getAttribute("name");

			actionEntity.setActionName(beanName);
			ActionSupport actionInstance = beanFactory.getBean(beanName);
			actionEntity.setActionInstance(actionInstance);
			actionEntity.setIsAjax(Boolean.valueOf(actionElement
					.getAttribute("ajax")));
			actionEntity.setIsValidPrivilege(Boolean.valueOf(actionElement
					.getAttribute("isValidPrivilege")));
			String loginType = actionElement.getAttribute("login");
			int intLoginType = StringUtil.isNullOrEmpty(loginType) ? 0
					: Integer.valueOf(loginType);
			actionEntity.setLoginType(intLoginType);
			Method method = null;
			Method[] methods = actionEntity.getActionInstance().getClass()
					.getMethods();
			String actionMethodName = actionElement.getAttribute("method");
			for (Method m : methods) {
				if (m.getName().equals(actionMethodName)) {
					method = m;
					break;
				}
			}
			// 获取所有参数名称列表
			actionEntity.setParameterNameList(MethodParameterNames
					.getParamNames(method));
			actionEntity.setMethod(method);
			NodeList resultList = actionElement.getChildNodes();
			for (int i = 0; i < resultList.getLength(); i++) {
				if (resultList.item(i).getNodeType() == 1) {
					Element resultElement = (Element) resultList.item(i);
					String resultName = resultElement.getAttribute("name");
					if (resultName.trim().toLowerCase().equals("direction")) {
						actionEntity.setDirectionUrl(resultElement
								.getTextContent().trim());

					} else if (resultName.trim().toLowerCase().equals("source")) {
						actionEntity.setSourceUrl(resultElement
								.getTextContent().trim());
					}
				}
			}
			Cache.getInstance().put(CACHE_KEY.ACTION_ENTITY, actionKey,
					actionEntity);

		} else {
			actionEntity = (ActionEntity) (oae);
		}
		return actionEntity;
	}
}
